package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.backend.dto.Response;
import org.example.backend.dto.TicketDTO;
import org.example.backend.dto.TicketOrderDTO;
import org.example.backend.entity.Ticket;
import org.example.backend.entity.TicketOrder;
import org.example.backend.service.TicketService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;

@Tag(name = "门票管理", description = "门票及订单管理相关接口")
@RestController
@RequestMapping("/tickets")
@RequiredArgsConstructor
public class TicketController {

    private final TicketService ticketService;

    @Operation(summary = "创建门票")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Ticket> createTicket(@Valid @RequestBody TicketDTO ticketDTO) {
        return Response.success(ticketService.createTicket(ticketDTO));
    }

    @Operation(summary = "更新门票")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Ticket> updateTicket(@PathVariable Long id, @Valid @RequestBody TicketDTO ticketDTO) {
        return Response.success(ticketService.updateTicket(id, ticketDTO));
    }

    @Operation(summary = "删除门票")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> deleteTicket(@PathVariable Long id) {
        ticketService.deleteTicket(id);
        return Response.success();
    }

    @Operation(summary = "获取门票详情")
    @GetMapping("/{id}")
    public Response<Ticket> getTicket(@PathVariable Long id) {
        return Response.success(ticketService.getTicket(id));
    }

    @Operation(summary = "获取门票列表")
    @GetMapping
    public Response<Page<Ticket>> getTickets(
            @RequestParam(required = false) Boolean isActive,
            Pageable pageable) {
        return Response.success(ticketService.getTickets(isActive, pageable));
    }

    @Operation(summary = "创建订单")
    @PostMapping("/orders")
    public Response<TicketOrder> createOrder(@Valid @RequestBody TicketOrderDTO orderDTO) {
        return Response.success(ticketService.createOrder(orderDTO));
    }

    @Operation(summary = "获取订单详情")
    @GetMapping("/orders/{id}")
    public Response<TicketOrder> getOrder(@PathVariable Long id) {
        return Response.success(ticketService.getOrder(id));
    }

    @Operation(summary = "获取当前用户的订单列表")
    @GetMapping("/orders/my")
    public Response<Page<TicketOrder>> getUserOrders(Pageable pageable) {
        return Response.success(ticketService.getUserOrders(pageable));
    }

    @Operation(summary = "获取所有订单列表")
    @GetMapping("/orders")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Page<TicketOrder>> getAllOrders(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            Pageable pageable) {
        return Response.success(ticketService.getAllOrders(startDate, endDate, pageable));
    }

    @Operation(summary = "取消订单")
    @PostMapping("/orders/{id}/cancel")
    public Response<Void> cancelOrder(@PathVariable Long id) {
        ticketService.cancelOrder(id);
        return Response.success();
    }
} 